//
//  main.m
//  ATM程序
//
//  Created by 钟纪旺 on 16/3/1.
//  Copyright © 2016年 钟纪旺. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>


//函数调用声明区
void window();
void operFunc(int oper);
void getMoney();
void outmoney();
void givemoney();
void changerMoney();
void changePassword();
void zhuanzhang();
void chaxun();
//全局变量声明区
//int sysPassword=999999;
int Money=10000;
int liu=8000;

void sMoney(){
    int Money=10000;
    FILE * fp  = fopen("/Users/zhongjiwang/Desktop/Money.txt", "r");
    if (!fp) {
        printf("余额文件加载失败......\n");
        exit(0);
    }
    fscanf(fp, "%d", &Money);
    fclose(fp);
}
void gMoney(){
    int onePsd=0;
    FILE * fp  = fopen("/Users/zhongjiwang/Desktop/Money.txt", "w");
    if (!fp) {
        printf("余额文件加载失败......\n");
        exit(0);
    }
    int i = fprintf(fp, "%d", onePsd);
    if (!i) {
        printf("余额写入失败！\n");
        exit(0);
    }else{
        printf("余额写入成功!\n");
        fclose(fp);

    }
}
//验证密码 欢迎界面
void checkPassword(){
    //从文件中获取原始密码
    int sysPassword2 = 0;
    FILE * fp  = fopen("/Users/zhongjiwang/Desktop/sysPassword.txt", "r");
    if (!fp) {
        printf("密码文件加载失败......\n");
        exit(0);
    }
    fscanf(fp, "%d", &sysPassword2);
    //printf("sysPassword2 = %d\n", sysPassword2) ;
    if (!sysPassword2) {
        int onePsd = 0, onePsdt = 0;
        printf("第一次登录，请设置原始密码：\n");
    loop:printf("请输入原始密码：");
        scanf("%d", &onePsd);
        printf("再输入原始密码：");
        scanf("%d", &onePsdt);
        if(onePsdt != onePsd)
        {
            printf("两次新密码设置不一样，请重新输入!\n");
            goto loop;
        }else{
            FILE * fp  = fopen("/Users/zhongjiwang/Desktop/sysPassword.txt", "w");
            if (!fp) {
                printf("密码文件加载失败......\n");
                exit(0);
            }
            int i = fprintf(fp, "%d", onePsd);
            if (!i) {
                printf("密码写入失败！\n");
                exit(0);
            }else{
                printf("密码写入成功!请重新登录\n");
                fclose(fp);
                checkPassword();
            }
        }
    }else{
        fclose(fp);
    }
   //定义输入的密码变量名
    int inPassword=0;
    int times=0;//控制输入的次数
    printf("---欢迎使用ATM机---\n");
    printf("请输入银行卡六位数密码：\n");
    scanf("%d",&inPassword);
    while(inPassword>999999||100000>inPassword)
    {
        printf("您输入的密码有误！请重新输入：\n");
        printf("请输入银行卡六位数密码：\n");
        scanf("%d",&inPassword);
    }
    times++;
    while (inPassword!=sysPassword2){
        //判断是不是第三次输入错误，如果错误直接退出程序
        if(times==3){
        
            printf("密码连续三次输入错误,您的卡已被冻结!\n");
            printf("请到银行办理激活手续!");
            exit(0);

        }
    
    //提示密码错误请重新输入
    printf("密码第%d次错误,还有%d次机会\n",times,3-times);
        printf("请输入银行卡密码:\n");
        scanf("%d",&inPassword);
    times++;
    }
    printf("恭喜您输入的密码正确！\n");
    window();
    

}
// 主界面函数
void window (){
    int oper=0;
    printf("欢迎进入ATM取款机\n");
    printf("----------------\n");
    printf("1-取款     2-存款\n");
    printf("3-转账     4-改密\n");
    printf("5-查询     6-退卡\n");
    printf("----------------\n");
    printf("请输入操作指令1-6:\n");
    scanf("%d",&oper);
    while(oper>6||oper<1){
        printf("输入错误!请重新输入:\n");
        printf("请输入操作指令1-6:\n");
        
    }
    operFunc(oper);
}
//指令处理函数
void operFunc(int oper){

    switch (oper) {
    case 1:
        //取款功能
        printf("取款进行中....请稍后....\n");
            getMoney();
        break;
            
        case 2:
        //存款功能
        
        printf("存款进行中....请稍后....\n");
            givemoney();
        break;

        case 3:
        //转账功能
        printf("转账进行中....请稍后....\n");
            zhuanzhang();
        break;

        case 4:
        //改密功能
        printf("改密进行中....请稍后....\n");
            changePassword();
        break;

        case 5:
        //查询功能
        printf("查询进行中....请稍后....\n");
            chaxun();
            break;

        case 6:
        //退卡功能
        printf("退卡进行中....请稍后....\n");
            printf("卡已退回,请取卡");
             exit(0);
        break;

    }
}
//  取款功能
void getMoney(){
    int money=0;
    int oper1=0;
    int oper2=0;
    printf("请选择取款金额\n");
    printf("1-100    2-200  \n");
    printf("3-500    4-1000 \n");
    printf("5-5000   6-手动输入\n");
    printf("7-退卡    8-返回主页面\n");
    scanf("%d",&oper1);
     if  (Money==0){
        printf("卡上余额为0，以自动返回主页面\n");
         window();
        
    }
    else if(oper1==1){
        int money=100;
        outmoney(money);
    }
    else if(oper1==2){
         int money=200;
        outmoney(money);
    }
    else if(oper1==3){
         int money=500;
        outmoney(money);
    }
    else if(oper1==4){
        int money=1000;
        outmoney(money);
    }
    else if(oper1==5){
        int money=5000;
        outmoney(money);
    }
   else if(oper1==6){
        printf("请输入您要取款的金额:");
        scanf("%d",&money);
       while (money%100!=0||money==0) {
           printf("输入有误!请重新输入:");
           scanf("%d",&money);
       }
      
       while (money>Money) {
           printf("余额不足");
           printf("已自动重新进入选择界面\n");
           getMoney();
       }
       Money=Money-money;
        printf("以取钱%d元余额%d元.\n",money,Money);
        }
  else  if(oper1==7){
       printf("卡已退回,请取卡");
        exit(0);
    }
  else  if(oper1==8){
      window();
      
  }

    
    printf("请继续选择服务项目\n");
    printf("1-继续取款\n");
    printf("2-返回主页面\n");
    printf("3-退卡\n");
    scanf("%d",&oper2);
    while(oper2>3||oper2<1){
        printf("输入错误!请重新输入:\n");
        printf("请输入操作指令1-3:\n");
        scanf("%d",&oper2);
       
    }
    if(oper2==1) {
        getMoney();
    }
    if(oper2==2) {
        window();
    }
    if(oper2==3) {
        printf("卡已退回,请取卡");
      exit(0);
    }
    }

//取款
void outmoney(int money){
    while (money>Money) {
        printf("余额不足,请重新输入");
        printf("已自动重新进入选择界面\n");
        getMoney();
    }        Money=Money-money;
    printf("以取钱%d元余额%d元:",money,Money);
}
//存款
void givemoney(){
    int money=0;
    int oper=0;
    printf("请输入存款金额\n");
    scanf("%d",&money);
    while (money%100!=0)
    
    {
        printf("输入有误!请重新输入");
        scanf("%d",&money);
    }
     printf("恭喜您!存款成功");
    Money=Money+money;
    printf("您的账户已存款%d元!余额%d元!\n",money,Money);
    printf("请继续选择操作\n");
    printf("1-继续存款  2-返回主页面\n");
    printf("3-退卡\n");
    scanf("%d",&oper);
    while(oper>3||oper<1){
        printf("输入错误!请重新输入:\n");
        printf("请输入操作指令1-3:\n");
        scanf("%d",&oper);
        
    }
    if(oper==1)
        givemoney();
    if(oper==2)
        window();
    if (oper==3) {
         exit(0);
    }
    

        }
//改密
void changePassword(){
    int times=0;
    int sysPassword1=0;
    int sysPassword3=0;
    int sysPassword4=0;
    int sysPassword2 = 0;
    int onePsd=0;
    FILE * fp  = fopen("/Users/zhongjiwang/Desktop/sysPassword.txt", "r");
    if (!fp) {
        printf("密码文件加载失败......\n");
        exit(0);
    }
    fscanf(fp, "%d", &sysPassword2);
    
        printf("请输入初始密码:\n");
    scanf("%d",&sysPassword1);
    while (sysPassword1>999999||100000>sysPassword1){
        printf("您输入的有误!请重新输入\n:");
        scanf("%d",&sysPassword1);
        }
    times++;
    while (sysPassword1!=sysPassword2){
        //判断是不是第三次输入错误，如果错误直接退出程序
        if(times==3){
            
            printf("密码连续三次输入错误,您的卡已被冻结!\n");
            printf("请到银行办理激活手续!");
            exit(0);
            
        }
        
        //提示密码错误请重新输入
        printf("原密码第%d次错误,还有%d次机会\n",times,3-times);
        printf("请输入银行卡密码:\n");
        scanf("%d",&sysPassword1);
        times++;
    }
    while (sysPassword1!=sysPassword2) {
        printf("密码输入错误，请重新输入:\n");
        scanf("%d",&sysPassword1);
    }
    while(sysPassword1==sysPassword2) {
        printf("密码验证成功!\n");
        fclose(fp);
        printf("请输入新密码\n");
       scanf("%d",&sysPassword4);
 while (sysPassword4>999999||100000>sysPassword4){
            printf("您输入的有误!请重新输入:\n");
            scanf("%d",&sysPassword4);
        }
        printf("请再次输入认证密码\n");
        scanf("%d",&sysPassword3);
    while (sysPassword4!=sysPassword3) {
        printf("您输入的密码不正确!请重新输入新密码：\n");
        scanf("%d",&sysPassword4);
        while (sysPassword4>999999||100000>sysPassword4){
            printf("您输入的有误!请重新输入:\n");
            scanf("%d",&sysPassword4);
        }
        printf("请再次输入认证密码\n");
        scanf("%d",&sysPassword3);

    }
        printf("恭喜您密码修改成功\n");
        sysPassword4=sysPassword3;
        onePsd=sysPassword4;
        FILE * fp  = fopen("/Users/zhongjiwang/Desktop/sysPassword.txt", "w");
        if (!fp) {
            printf("密码文件加载失败......\n");
            exit(0);
        }
        int i = fprintf(fp, "%d", onePsd);
        if (!i) {
            printf("密码写入失败！\n");
            exit(0);
        }else{
            printf("密码写入成功!请重新登录\n");
            fclose(fp);
        }
        printf("请重新登陆\n");
        checkPassword();
       
    }

}
//转账
void zhuanzhang(){
    int money;
    int oper;
    printf("请输入转账金额：");
    scanf("%d",&money);
    while (money%100!=0||money==0) {
        printf("输入有误!请重新输入:\n");
        scanf("%d",&money);
        }
    while (money>Money) {
        printf("您的余额不足!以自动跳入主页面\n");
        window();
    }
    
    Money=Money-money;
    liu=liu+money;
    printf("恭喜您转账成功%d元!,余额%d元\n",money,Money);
    printf("欢迎您下次使用,请选择服务项目\n");
    printf("1－继续转账    2-返回主页面\n");
    scanf("%d",&oper);
    while (oper>2||oper<1) {
        printf("输入有误,请重新输入");
        scanf("%d",&oper);
    }
    if(oper==1){
        zhuanzhang();
    
    }
    else if(oper==2)
    {
    
        window();
    }
}
//chaxun
void chaxun(){
    int oper=0;
    printf("您的帐号余额%d元\n",Money);
    printf("请继续选择您的选择\n");
    printf("1-返回主页面   2-退卡\n");
    scanf("%d",&oper);
    while (oper>2||oper<1) {
        printf("输入有误,请重新输入");
        scanf("%d",&oper);
    }
    printf("1-返回主页面   2-退卡/n");
    if(oper==1){
        window();
        
    }
    else if(oper==2)
    {
        
        exit(0);
    }

}
int main()
{
    checkPassword();
}